﻿# FindHaru
# --------
#
# Find the Haru libraries(Only for Haru provided by FastCAE)
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# The following variables will be defined:
#
# ``Haru_FOUND`` True if Haru found on the local system
#
# ``Haru_VERSION`` Haru Version(x.x.x)
#
# ``Haru_VERSION_MAJOR`` Haru Major Version
#
# ``Haru_VERSION_MINOR`` Haru Minor Version
#
# ``Haru_VERSION_PATCH`` Haru Patch Version
#
# ``Haru_DIRS`` Location of Haru(root dir)
#
# ``Haru_INCLUDE_DIRS`` Location of Haru header files
#
# ``Haru_LIBRARY_DIRS`` Location of Haru libraries
#
# ``Haru_LIBRARIES`` List of the Haru libraries found
#

# 防止重复引入
if(FASTCAE_HARU_ALREADY_INCLUDED)
	return()
endif()
set(FASTCAE_HARU_ALREADY_INCLUDED 1)

# find_path 搜索包含某个文件的路径
# 如果在某个路径下发现了该文件，该结果会被存储到该变量中；如果没有找到，存储的结果将会是<VAR>-NOTFOUND
find_path(Haru_DIRS 
	NAMES 
		include/hpdf.h 
	PATHS 
		${CMAKE_SOURCE_DIR}/extlib/Haru
	NO_SYSTEM_ENVIRONMENT_PATH
	NO_CMAKE_SYSTEM_PATH
)

set(Haru_VERSION_MAJOR 2)
set(Haru_VERSION_MINOR 4)
set(Haru_VERSION_PATCH 3)

set(Haru_VERSION "${Haru_VERSION_MAJOR}.${Haru_VERSION_MINOR}.${Haru_VERSION_PATCH}")

find_path(Haru_INCLUDE_DIRS
	NAMES
		hpdf.h
	HINTS
		${Haru_DIRS}/include
)

find_path(Haru_LIBRARY_DIRS
	NAMES
		hpdf.lib libhpdf.so
	HINTS
		${Haru_DIRS}/lib
)

set(Haru_LIBRARIES FASTCAE::HARU)

add_library(FASTCAE::HARU SHARED IMPORTED)
set_property(TARGET FASTCAE::HARU PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Haru_INCLUDE_DIRS})
set_property(TARGET FASTCAE::HARU APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
	set_target_properties(FASTCAE::HARU PROPERTIES
		IMPORTED_LOCATION_RELEASE "${Haru_LIBRARY_DIRS}/libhpdf.so"
		#IMPORTED_SONAME_RELEASE "${Haru_LIBRARY_DIRS}/libHaru.so"
	)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
	set_target_properties(FASTCAE::HARU PROPERTIES
		IMPORTED_IMPLIB_RELEASE "${Haru_LIBRARY_DIRS}/hpdf.lib"
		IMPORTED_LOCATION_RELEASE "${Haru_DIRS}/bin/hpdf.dll"
	)
endif()

include(FindPackageHandleStandardArgs)

# 如果找到所有需要的变量，并且版本匹配，则将Haru_FOUND变量设置为TRUE
find_package_handle_standard_args(Haru
	FOUND_VAR
		Haru_FOUND
	REQUIRED_VARS
		Haru_DIRS
		Haru_INCLUDE_DIRS
		Haru_LIBRARY_DIRS
		Haru_LIBRARIES
	VERSION_VAR
		Haru_VERSION
)